package com.shy.config.util.convert;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.shy.entity.DicValue;
import com.shy.service.DicValueService;

import java.util.List;

/**
 * 意向状态转换器
 */
public class IntentionStateConverter implements Converter<Integer> {


    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData,
                                     ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) throws Exception {
        // 获取单元格内容
        String cellValue = cellData.getStringValue();
        // 获取spring容器中指定的bean对象
        DicValueService dicValueService = AppellationContextHolder.getBean(DicValueService.class);

        // 根据类型编码查询数据字典值集合
        List<DicValue> dicValueList = dicValueService.queryDicValueByCode("intentionState");

        // 循环遍历数据字典值集合，判断编码是否相等，如果相等则返回对应的值
        DicValue dicValue = dicValueList.stream()
                .filter(dv -> cellValue.equals(dv.getTypeValue())).toList().getFirst();

        // 判断是否为空
        if (ObjUtil.isNotEmpty(dicValue)) {
            return dicValue.getId();
        }
        return -1;
    }
}
